// 全部商品  排他
function paita(article) {
    $(article).mouseover(function () {
        $(this).addClass('active').parent().siblings().children().removeClass('active')
    })
}

$('.search-list a').mouseover(function () {
    // 鼠标悬停对应字母下标
    var ind = $(this).index()
    // console.log(ind);

    // 找到对应下标的li
    // console.log($('.countrys ul li').eq(ind));
    // 找到对应下标的li到顶部距离
    var distance = 0 - Number($('.countrys ul li').eq(ind).position().top)
    // console.log(distance);

    var cityTop = $('.city-con .list-con .countrys ul')
    cityTop.stop().animate({ top: distance }, 200, 'linear')

    // 滚轮移动距离
    var gunlun = ind * 5
    $('.list-con .wheel b').stop().animate({ top: gunlun }, 200, 'linear')
});

// $('.list-con .wheel b').mousedown()
// 输入框获取焦点不变化

$('.but .search').focus(function(){
    $(this).css('outline','0')
})

// 排他
var article = document.querySelectorAll('.lists li a')
paita(article)




// 全部商品隐藏出现
$('.index_main_l>ul>li').hover(function () {
    $(this).addClass('active').siblings().removeClass('active');
    $('.main_tab_cont').removeClass('no');
    $(this).find('.main_tab_cont').addClass('no');

}, function () {
    $('.index_main_l>ul>li').removeClass('active')
    $('.main_tab_cont').removeClass('no');
})


// 点击删除当前商品
$('.shopcar .shop-dis ul li').on('click','.del',function(){
    $(this).parents('li').remove()
    totale()
    if($('.shopcar .shop-dis ul li').length==0){
        var carts = ` <div class="dis-img">
        <img src="../images/cart-nogoods.png" alt="">
        <span>
            购物车中还没有商品，赶紧选购吧。
        </span>
    </div>`;
    $('.shop-dis').html(carts);
    }
})



function shopping() {
    var carts = '';
    var carsData = JSON.parse(localStorage.getItem('shoppingCart'));

    if (carsData == null || carsData.length == 0) {
        carts = ` <div class="dis-img">
        <img src="../images/cart-nogoods.png" alt="">
        <span>
            购物车中还没有商品，赶紧选购吧。
        </span>
    </div>`;
    $('.adis-con').html(carts);
    } else {
        for (var i = 0; i < carsData.length; i++) {
            carts += `<li>
            <img src="${carsData[i].pictureURL}" alt="">
            <a href="">${carsData[i].Hometitle}</a>
            <div class="nums">
                <span>1</span>
                <i class="add">^</i>
                <i class="reduce">^</i>
            </div>
            <div class="nums-r">
                <span class="price">${carsData[i].Homeprice}</span>
                <span class="del">删除</span>
            </div>
        </li>`
        };

        var temp =`
        <ul></ul>
        <div class="payto">
                    <p>
                        共<span class="numb">1</span>件商品&nbsp;&nbsp;
                        共计：<span class="total"></span>
                    </p>

                    <a href="./shoppingCart.html">去购物车</a>
                </div>`
                $('.shop-dis').one().html(temp)  
                $(carts).appendTo('.shop-dis ul');
    }
};
shopping();
$('.btnAppend').click(function() {
    shopping();
    totale()
    $('.shop-num').text($('.buyNum').val());
});



function totale(){
    // 商品总数
    var shu = 0;
    // 商品总价
    var pric=0;

    $('.shopcar .shop-dis ul li').each(function(index,value){
        shu +=Number($(this).find('.nums span').text())
        pric +=Number($(this).find('.nums span').text()) * Number($(this).find('.price').text())
        console.log( Number($(this).find('p').text()))
    });
    console.log(shu);
   
    $('.shopcar .shop-dis .payto p .numb').text(shu)
    $('.shopcar .shop-dis .payto p .total').text(pric)

    $('.shopcar .my-shop em').text(shu)
}
totale()


$('.shopcar .shop-dis ul li').on('click','.add',function(){
    var sun =Number($(this).prev().text())
    sun++;
    $(this).prev().text(sun)
    totale()
})
$('.shopcar .shop-dis ul li').on('click','.reduce',function(){
    var sun =Number($(this).prev().prev().text())
    sun--;
    if(sun<=0){
        // $(this).parents('ul').next().remove()
        $(this).parents('li').remove()
    }
    $(this).prev().prev().text(sun)
    totale()
    shopping();
})

function panduan(){
    if($('.shopcar .shop-dis ul li').length==0||$('.shopcar .shop-dis ul').length==0){
        var carts = ` <div class="dis-img">
        <img src="../images/cart-nogoods.png" alt="">
        <span>
            购物车中还没有商品，赶紧选购吧。
        </span>
    </div>`;
    $('.shop-dis').html(carts);
  }
}

panduan()